package org.afun.csms_android;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.afun.csms_android.domain.Course;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;

public class CourseAddActivity extends AppCompatActivity {
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_add);
        mContext = this;
        init();

    }

    private void init() {
        EditText couNameEdit = findViewById(R.id.couName);
        EditText couCreditEdit = findViewById(R.id.coucredit);
        Button addCourseOK = findViewById(R.id.addCourseOk);
        Button cancelAddCourse = findViewById(R.id.cancelAddCourse);

        addCourseOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String couName = couNameEdit.getText().toString();
                String credit = couCreditEdit.getText().toString();
                Course course = new Course();
                course.setCouName(couName);
                course.setCredit(Integer.parseInt(credit));
                MyTableOperator myTableOperator = new MyTableOperator(mContext);
                TableOperator tableOperator = myTableOperator.getTableOperator();
                boolean b = tableOperator.addCourse(course);
                if (b) {
                    new AlertDialog.Builder(mContext)
                            .setTitle("提示信息")
                            .setMessage("添加成功！")
                            .setPositiveButton("确定", null)
                            .create()
                            .show();

                } else {
                    new AlertDialog.Builder(mContext)
                            .setTitle("提示信息")
                            .setMessage("添加失败...")
                            .setPositiveButton("确定", null)
                            .create()
                            .show();
                }
            }
        });

        cancelAddCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}